package com.example.hadoop.mapreduce.comonfriend;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Brian
 * @Date: 2020/04/26/8:24
 * @Description: B-C	A
 * B-D	A
 * B-F	A
 * B-G	A
 * B-H	A
 * B-I	A
 * <p>
 * a-b :  c ,e
 */
public class GroupFriendSepTwo {
    static class GroupFriendSepTwoMapper extends Mapper<LongWritable, Text, Text, Text> {
        Text k = new Text();
        Text v = new Text();

        /**
         * B-C	A
         * B-D	A
         * B-F	A
         * B-G	A
         * B-H	A
         * B-I	A
         *
         * @param key
         * @param value
         * @param context
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] splits = line.split("\t");
            k.set(splits[0]);
            v.set(splits[1]);
            context.write(k, v);
        }
    }

    static class GroupFriendSepTwoReducer extends Reducer<Text, Text, Text, Text> {
        Text v = new Text();

        /**
         * @param key
         * @param values
         * @param context
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            List<String> commonFriends = new ArrayList<>();
            for (Text text : values) {
                commonFriends.add(text.toString());
            }
            StringBuffer sb = new StringBuffer();
            for (String friend : commonFriends) {
                sb.append(friend).append(",");
            }
            if (sb.toString().length() > 0) {
                String friend = sb.deleteCharAt(sb.length() - 1).toString();
                v.set(friend);
                context.write(key, v);
            }
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        if (args == null || args.length < 2) {
            args[0] = "/wordcount/flowinput";
            args[1] = "/wordcount/flowoutput";
        }
        //加载默认配置
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        //当程序运行时，通过类加载器获取类的所在路径
        job.setJarByClass(GroupFriendSepTwo.class);
        //配置mapper和reducer类
        job.setMapperClass(GroupFriendSepTwoMapper.class);
        job.setReducerClass(GroupFriendSepTwoReducer.class);

        //配置map的输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        //配置最终输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputKeyClass(Text.class);

        Path outputPath = new Path(args[1]);
        FileSystem fileSystem = FileSystem.get(new Configuration());
        if (fileSystem.exists(outputPath)) {
            fileSystem.delete(outputPath, true);
        }
        //设置数据源路径和输入路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, outputPath);

        //将Job提交给Yarn
        boolean successfully = job.waitForCompletion(true);
        System.out.println("Successfully? --> " + successfully);
        System.exit(successfully ? 0 : 1);
    }
}
